using System;
using miPics.Common;
using miPics.Rss;


public partial class _Administration_EditImage : miPics.BasePage.BasePage
{
	private bool ValidImage = false;
	private short ImageID;
	private short GalleryID;

	protected void Page_Load(object sender, EventArgs e)
	{
		// set menu administration task
		Master.SetMenuAdministrationTask("Edit image");

		// load image we are editing
		this.ImageID = ToolKit.ReturnShort(Request.QueryString["id"]);

		// get image details into datatable through table adapter
		miPicsDataAccessTableAdapters.ImageItemTableAdapter ImageItemTableAdapter = new miPicsDataAccessTableAdapters.ImageItemTableAdapter();
		miPicsDataAccess.ImageItemDataTable ImageItemDataTable = ImageItemTableAdapter.GetData(this.ImageID, this.IsLoggedIn);

		// if image was loaded, show edit interface
		this.ImageLoaded = (ImageItemDataTable.Count == 1);

		if (ImageItemDataTable.Count == 1)
		{
			this.ValidImage = true;

			// place image record into item row object
			miPicsDataAccess.ImageItemRow ImageItemRow = (miPicsDataAccess.ImageItemRow)ImageItemDataTable.Rows[0];

			this.GalleryID = ImageItemRow.GalleryID;

			// if not postback, populate fields with image data
			if (!this.IsPostBack)
			{
				// pass image details to controls
				_Image.Src = "~/_Resource/Image/" + ImageItemRow.Thumbnail;
				_Image.Width = ImageItemRow.ThumbnailWidth;
				_Image.Height = ImageItemRow.ThumbnailHeight;

				// pass image description details
				_ImageName.Value = ImageItemRow.Name;
				_Image.Alt = ToolKit.GetAltImageName(ImageItemRow.Name);
				_ImageDescription.Value = ImageItemRow.Description;

				// populate sequence dropdown
				this.PopulateSequenceDropDown(this.GalleryID, ImageItemRow.SequenceActual);

				// set the image enabled checkbox
				_ImageEnabled.Checked = ImageItemRow.Enabled;

				// if image is gallery image then hide "set gallery image" checkbox and show "Is gallery image" message
				this.IsGalleryImage = (this.ImageID == ImageItemRow.GalleryImageID);
			}
		}
	}

	private void PopulateSequenceDropDown(short GalleryID, short ImageSequence)
	{
		// work out number of images in the gallery to build sequence selector
		miPicsDataAccessTableAdapters.GalleryItemTableAdapter GalleryItemTableAdapter = new miPicsDataAccessTableAdapters.GalleryItemTableAdapter();
		short ImageCount = (short)GalleryItemTableAdapter.GetImageCount(GalleryID, true);

		// populate sequence select box with options
		for (int Counter = 1; Counter <= ImageCount; Counter++)
			_ImageSequence.Items.Add(Counter.ToString());

		// select the current sequence this image has
		_ImageSequence.Items[ImageSequence - 1].Selected = true;
	}

	protected void UpdateImage(object sender, EventArgs e)
	{
		// update image details to database
		if (this.ValidImage)
		{
			// commit details to image record
			miPicsDataAccessTableAdapters.ImageItemTableAdapter ImageItem = new miPicsDataAccessTableAdapters.ImageItemTableAdapter();

			ImageItem.UpdateItem(
				this.ImageID,
				ToolKit.TrimToLength(_ImageName.Value, 100),
				_ImageEnabled.Checked,
				ToolKit.TrimToLength(_ImageDescription.Value, 1000),
				ToolKit.ReturnShort(_ImageSequence.Value, 1),
				null, null, null,
				null, null, null);

			// if user has selected the "set as gallery image" option then set gallery image to this image
			if (_IsGalleryImage.Checked)
			{
				miPicsDataAccessTableAdapters.GalleryItemTableAdapter GalleryItem = new miPicsDataAccessTableAdapters.GalleryItemTableAdapter();
				GalleryItem.UpdateItemGalleryImage(this.GalleryID, this.ImageID);
			}

			// update gallery rss
			WriteRss GalleryRss = new WriteRss(this.Request);
			GalleryRss.SaveRss();

			// redirect back to the gallery
			this.RedirectToGallery();
		}
	}

	protected void CancelUpdate(object sender, EventArgs e)
	{
		this.RedirectToGallery();
	}

	private void RedirectToGallery()
	{
		if (this.ValidImage)
			Response.Redirect("~/Gallery/?id=" + this.GalleryID.ToString());
	}

	private bool ImageLoaded
	{
		set
		{
			_ErrorMessage.Visible = !value;
			_Interface.Visible = value;
		}
	}

	private bool IsGalleryImage
	{
		set
		{
			_IsGalleryImageInterface.Visible = !value;
			_IsGalleryImageMessage.Visible = value ;
		}
	}
}
